﻿namespace Journey.Events
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// 事件总线容器。
    /// </summary>
    public static class EventManagerContainer
    {
        /// <summary>
        /// 实例
        /// </summary>
        public static IDictionary<Type, object> Instance = new Dictionary<Type, object>();

        /// <summary>
        /// 获得事件管理器实例。
        /// </summary>
        /// <typeparam name="TEvent">事件类型。</typeparam>
        /// <returns>事件管理器实例。</returns>
        public static IEventManager<TEvent> GetInstance<TEvent>() where TEvent : class, IEvent
        {
            Type key = typeof(TEvent);

            if (Instance.ContainsKey(key))
            {
                return Instance[key] as IEventManager<TEvent>;
            }

            IEventManager<TEvent> eventManager = new DefaultEventManager<TEvent>();

            Instance[key] = eventManager;

            return eventManager;
        }
    }

    /// <summary>
    /// 事件总线容器。
    /// </summary>
    /// <typeparam name="TEvent">事件类型。</typeparam>
    public static class EventManagerContainer<TEvent> where TEvent : class, IEvent
    {
        /// <summary>
        /// 实例。
        /// </summary>
        public static IEventManager<TEvent> Instance { get; set; }
    }
}